package com.zyg.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.order.enttiy.Cart;
import com.zyg.order.enttiy.TbAddress;
import com.zyg.order.enttiy.TbOrder;
import com.zyg.order.enttiy.TbOrderItem;
import com.zyg.order.service.OrderService;
import com.zyg.order.service.TbAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * ------------------------------
 * 作者: LMY
 * 类-功能: XX
 * 创建时间: 2021-11-18 01:33
 * ------------------------------
 **/
@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private TbAddressService addressService;


    @GetMapping({"/","orderInfo.html"})
    public String orderInfo(Model model){
        //1. 拿到登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //2. 取出购物车
        List<Cart> cartList =  orderService.getCartList(name);
        //3. 将购物车放入model中
        model.addAttribute("cartList",cartList);

        //4. 根据用户名查出对应的地址信息
        List<TbAddress> addressList = addressService.list(new QueryWrapper<TbAddress>().eq("user_id", name));
        model.addAttribute("addressList",addressList);

        return "orderInfo";
    }

    /**
     * 作者 : LMY
     * 功能 : 保存订单
     * 时间 : 2021/11/18 20:48
     */
    @PostMapping("/order/add")
    public String addOrder(TbOrder order){
        //1. 拿到登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //2. 保存订单
        orderService.saveOrder(name,order);

        return "orderInfo";
    }





}
